import 'dart:io';

import 'package:logger/logger.dart';
import 'package:sqflite/sqflite.dart';
import 'package:sqflite_common_ffi/sqflite_ffi.dart';

import '../util/DbUtil.dart';
import '../util/ImSingleton.dart';

class BaseDao {

  // late Database _database;
  final _logger = Logger(printer: PrettyPrinter(),);

  BaseDao();

  Database? getDatabase() {
    return ImSingleton.getInstance()?.dataBase;
  }

  // Future<Database> getReadOnlyDatabase() async {
  //   var startTime = DateTime.now();
  //   Database database = await DbUtil.getReadOnlyDatabase();
  //   var time = DateTime.now().millisecond - startTime.millisecond;
  //   _logger.i('db get readonly time: $time');
  //   return database;
  // }

  _transaction(Function function) {
    Database? database = getDatabase(); //ImSingleton.getInstance()?.dataBase;
    Batch? batch = null;
    try {
      batch = database?.batch();
      function(batch);
      batch?.commit();
    } catch(e) {
      throw e;
    }
  }

  String _inParams(List<Object> list) {
    return list.map((p)=> "?").join(",");
  }
}